package com.example.blank700.handlerthread;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class HandlerThread extends AppCompatActivity implements View.OnClickListener {
    private Button btn_start=null;
    private Button btn_end=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_thread);

        btn_start=(Button)findViewById(R.id.btn_start);
        btn_start.setOnClickListener(this);
        btn_end=(Button)findViewById(R.id.btn_end);
        btn_end.setOnClickListener(this);
    }

    Handler handler=new Handler();

    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_start:
                handler.post(updateThread);
                break;
            case R.id.btn_end:
                handler.removeCallbacks(updateThread);
                break;
        }
    }

    Runnable updateThread=new Runnable() {
        @Override
        public void run() {
            System.out.println("UpdateThread");
            Log.v("tag","UpdateThread");
            //延时
            handler.postDelayed(updateThread,2000);
        }
    };
}
